package com.hk.yingmi;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.commons.JsonUtils;
import com.hk.commons.vo.FundInfoVoNav;
import com.hk.res.KeyRes;
import com.hk.service.IFundInfo_Yimi;
import com.hk.service.IFundNav;

@Controller
@RequestMapping(value = "/fundsearch")
public class FundSearchCtrl {

	private Logger logger = LoggerFactory.getLogger(FundSearchCtrl.class);

	@Autowired
	private IFundInfo_Yimi fundInfoService;

	@Autowired
	private IFundNav fundNavService;

	@ResponseBody
	@RequestMapping(value = "/fundInfoList", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public String searchList(@RequestBody KeyRes res) {
	/*	ResponseBase<List<FundInfoVoNav>> rb = new ResponseBase<>();*/
		String key = res.getKeys();
		logger.info("/fundsearch/fundInfoList params: key {}", key);

		/*HqlFilter hqlFilter = new HqlFilter();
		List<FundInfoVoNav> resultList = new ArrayList<>();
		if (StringUtils.isNumeric(key)) {
			hqlFilter.addEQFilter("fundCode", key);
		} else {
			hqlFilter.addEQFilter("fundFullName", key);
		}
		*/
		/*long size = fundInfoService.countByFilter(hqlFilter);*/
		int firstResult = (res.getCurPage() - 1) * res.getPageSize();
		int maxSize = res.getPageSize();

		String hql = "Select new com.hk.commons.vo.FundInfoVoNav(fi.fundFullName,fn.navDate,fn.fundCode , fn.nav , fn.oneWeekReturn , fn.dayReturn , fn.unitYield , fn.yearlyRoe , fi.fundType)from FundInfo_Yimi fi , FundNav fn where fi.fundCode = fn.fundCode "
					+ "and fi.onSale = 1 ";
		StringBuffer buf = new StringBuffer();
		buf.append(hql);
		
		if (StringUtils.isNumeric(key)) {
			buf.append(" and ");
			buf.append("fi.fundCode Like '%");
			buf.append(key);
			buf.append("%'");
		} else {
			buf.append(" and ");
			buf.append("fi.fundFullName Like '%");
			buf.append(key);
			buf.append("%'");
		}
		
		List<FundInfoVoNav> list = fundNavService.executeListHql(
				String.valueOf(buf), firstResult, maxSize);

		/*rb.setSize(size);
		rb.setResult(resultList);*/

		return JsonUtils.toJson(list);
	}

}
